/* eslint-disable */
module.exports = function (redisClient, module) {
    module.sortedSetIntersectCard = function (keys, callback) {
        if (!Array.isArray(keys) || !keys.length) {
            return callback(null, 0);
        }
        const tempSetName = `temp_${Date.now()}`;

        const interParams = [tempSetName, keys.length].concat(keys);

        const multi = redisClient.multi();
        multi.zinterstore(interParams);
        multi.zcard(tempSetName);
        multi.del(tempSetName);
        multi.exec((err, results) => {
            if (err) {
                return callback(err);
            }
            callback(null, results[1] || 0);
        });
    };

    function getSortedSetRevIntersect(params, callback) {
        const sets = params.sets;
        const start = params.start ? params.start : 0;
        const stop = params.stop ? params.stop : -1;
        const weights = params.weights || [];

        const tempSetName = `temp_${Date.now()}`;

        let interParams = [tempSetName, sets.length].concat(sets);
        if (weights.length) {
            interParams = interParams.concat(['WEIGHTS'].concat(weights));
        }

        if (params.aggregate) {
            interParams = interParams.concat(['AGGREGATE', params.aggregate]);
        }

        const rangeParams = [tempSetName, start, stop];
        if (params.withScores) {
            rangeParams.push('WITHSCORES');
        }

        const multi = redisClient.multi();
        multi.zinterstore(interParams);
        multi[params.method](rangeParams);
        multi.del(tempSetName);
        multi.exec((err, results) => {
            if (err) {
                return callback(err);
            }

            if (!params.withScores) {
                return callback(null, results ? results[1] : null);
            }
            results = results[1] || [];
            const objects = [];
            for (let i = 0; i < results.length; i += 2) {
                objects.push({ value: results[i], score: parseFloat(results[i + 1]) });
            }
            callback(null, objects);
        });
    }

    module.getSortedSetIntersect = function (params, callback) {
        params.method = 'zrange';
        getSortedSetRevIntersect(params, callback);
    };

    module.getSortedSetRevIntersect = function (params, callback) {
        params.method = 'zrevrange';
        getSortedSetRevIntersect(params, callback);
    };
};
/* eslint-enable */
